package com.ray.depot.service.impl;

import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.ray.auth.system.emun.UseStatus;
import com.ray.depot.service.LibraryService;
import com.ray.depot.service.MaterialLibraryService;
import com.ray.depot.table.customMapper.SelectLibraryMapper;
import com.ray.depot.table.dto.LibraryDto;
import com.ray.depot.table.entity.Library;
import com.ray.depot.table.entity.LibraryExample;
import com.ray.depot.table.entity.LibraryExample.Criteria;
import com.ray.depot.table.mapper.LibraryMapper;
import com.xiaoleilu.hutool.util.StrUtil;
import com.zjhc.flyhong.common.function.enums.DeleteEnum;
import com.zjhc.flyhong.common.function.exception.BussinessException;
import com.zjhc.flyhong.common.function.operator.LoginUser;
import com.zjhc.flyhong.common.function.operator.PageDto;
import com.zjhc.flyhong.common.util.UUIDUtil;
import com.zjhc.flyhong.common.web.ErrorMsg;
import com.zjhc.flyhong.common.web.Result;
import com.zjhc.flyhong.common.web.SuccessMsg;
import com.zjhc.flyhong.common.web.base.BaseService;

@Service
public class LibraryServiceImpl extends BaseService implements LibraryService {
	/** 日志对象 **/
	private static Logger logger = Logger.getLogger(LibraryServiceImpl.class);

	@Autowired
	private LibraryMapper libraryMapper;
	@Autowired
	private SelectLibraryMapper selectLibraryMapper;
	@Autowired
	private MaterialLibraryService materialLibraryService;

	@Transactional
	public Result<Object> addLibrary(Library params, LoginUser user) {
		Result<Object> result = null;
		result = new Result<Object>(0, SuccessMsg.OP_SUCCESS);

		params.setTenantId(user.getTenantId());
		params.setLibraryId(UUIDUtil.getUUID());
		params.setIsDelete(DeleteEnum.USE.getValue());
		this.setUserForAdd(params, user);
		int count = libraryMapper.insertSelective(params);
		if (count != 1) {
			logger.error("LibraryServiceImpl=>addLibrary is error");
			throw new BussinessException(-1, ErrorMsg.INSERT_ERROR);
		}
		return result;
	}


	@Transactional(propagation = Propagation.NOT_SUPPORTED)
	public Library queryLibrary(String id, LoginUser user) {
		return libraryMapper.selectByPrimaryKey(id);
	}

	@Transactional
	public Result<Object> updateLibrary(Library params, LoginUser user) {
		Result<Object> result = null;
		result = new Result<Object>(0, SuccessMsg.OP_SUCCESS);
		this.setUserForUpdate(params, user);
		int count = libraryMapper.updateByPrimaryKeySelective(params);
		if (count != 1) {
			logger.error("LibraryServiceImpl=>editLibrary is error");
			throw new BussinessException(-1, ErrorMsg.INSERT_ERROR);
		}
		return result;
	}

	@Transactional
	public Result<Object> deleteLibrary(String id, LoginUser user) {
		Result<Object> result = null;
		result = new Result<Object>(0, SuccessMsg.OP_SUCCESS);
		Library params = new Library();
		params.setLibraryId(id);
		params.setIsDelete(DeleteEnum.DELETE.getValue());
		this.setUserForUpdate(params, user);
		int count = libraryMapper.updateByPrimaryKeySelective(params);
		if (count != 1) {
			logger.error("LibraryServiceImpl=>deleteLibrary is error");
			throw new BussinessException(-1, ErrorMsg.INSERT_ERROR);
		}
		return result;
	}

	@Transactional(propagation = Propagation.NOT_SUPPORTED)
	public PageDto<Library> queryLibrary(Library params, PageDto<Library> page, LoginUser user) {
		LibraryExample example = new LibraryExample();
		Criteria criteria = example.createCriteria();
		// 超级管理员能查看全部
		if (!user.isSuperManager()) {
			criteria.andTenantIdEqualTo(user.getTenantId());
		}
		//仓库ID
		if(StrUtil.isNotBlank(params.getWarchouseId())){
			criteria.andWarchouseIdEqualTo(params.getWarchouseId());
		}
		
		criteria.andIsDeleteEqualTo(DeleteEnum.USE.getValue());
		this.setExamplePage(example, page);
		page.setTotal(libraryMapper.countByExample(example));
		page.setRecords(libraryMapper.selectByExample(example));
		return page;
	}

	@Transactional
	@Override
	public void deleteLibraryByWarehouseId(String id, LoginUser user) {
		Library params = new Library();
		params.setIsDelete(DeleteEnum.DELETE.getValue());
		this.setUserForUpdate(params, user);
		LibraryExample example = new LibraryExample();
		Criteria criteria = example.createCriteria();
		criteria.andWarchouseIdEqualTo(id);
		//查询库位
		List<Library> libraries = libraryMapper.selectByExample(example);
		//删除库位
		libraryMapper.updateByExampleSelective(params, example);
		
		//删除库位对应的 库位物料关联
		for(Library library : libraries){
			materialLibraryService.deleteMaterialLibraryByLibraryId(library.getLibraryId(), user);
		}
		
	}
	
	@Transactional(propagation = Propagation.NOT_SUPPORTED)
	public PageDto<LibraryDto> querySelectLibrary(Library params,String materialId, PageDto<LibraryDto> page, LoginUser user) {
		LibraryExample example = new LibraryExample();
		Criteria criteria = example.createCriteria();
		// 超级管理员能查看全部
		if (!user.isSuperManager()) {
			criteria.andTenantIdEqualTo(user.getTenantId());
		}
		//仓库ID
		if(StrUtil.isNotBlank(params.getWarchouseId())){
			criteria.andWarchouseIdEqualTo(params.getWarchouseId());
		}
		criteria.andLibraryStatusEqualTo(UseStatus.ON.getType());
		criteria.andIsDeleteEqualTo(DeleteEnum.USE.getValue());
		this.setExamplePage(example, page);
		page.setTotal(selectLibraryMapper.countByExample(example));
		page.setRecords(selectLibraryMapper.selectByExample(example,materialId));
		return page;
	}
	
}
